/*
 * Copyright 2010-2011 Mike Novak <michael.novakjr@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.codeversed.plugins.buzz.util;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.net.http.AndroidHttpClient;
import android.util.Log;

import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;

import com.codeversed.plugins.buzz.util.Constants;

public class ImageUtils {

    public static Bitmap roundBitmapCorners(Resources res, int id, int px) {
        Bitmap bitmap = BitmapFactory.decodeResource(res, id);
        return round(bitmap, px);
    }
    
    public static Bitmap roundBitmapCorners(String file, int px) {
        Bitmap bitmap = BitmapFactory.decodeFile(file);
        return round(bitmap, px);
    }
    
    public static Bitmap roundBitmapCorners(Bitmap bitmap, int px) {
        return round(bitmap, px);
    }
    
    private static Bitmap round(Bitmap bitmap, int px) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        
        Canvas canvas = new Canvas(output);
        
        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = px;

        paint.setAntiAlias(true);
        
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        
        final Paint strokePaint = new Paint();
        strokePaint.setAntiAlias(true);
        strokePaint.setStyle(Paint.Style.STROKE);
        strokePaint.setColor(0xffffffff);
        strokePaint.setStrokeWidth(2.0f);
        
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        //canvas.drawRoundRect(rectF, roundPx, roundPx, strokePaint);
        
        return output;
    }
    
    public static Bitmap downloadBitmap(String url) {
        final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
        final HttpGet get = new HttpGet(url);
        
        try {
            HttpResponse response = client.execute(get);
            final int statusCode = response.getStatusLine().getStatusCode();
            
            if (statusCode != HttpStatus.SC_OK) {
                Log.w(Constants.LOG_TAG, "Error: " + statusCode + " while downloading image: " + url);
                return null;
            }
            
            final HttpEntity entity = response.getEntity();
            
            if (entity != null) {
                InputStream in = null;
                
                try {
                    in = entity.getContent();
                    final Bitmap bitmap = BitmapFactory.decodeStream(in);
                    
                    return bitmap;
                } finally {
                    if (in != null) {
                        in.close();
                    }
                    
                    entity.consumeContent();
                }
            }
        } catch (Exception e) {
            Log.w(Constants.LOG_TAG, "Error retrieving image: " + url);
            get.abort();
        } finally {
            if (client != null) {
                client.close();
            }
        }
        
        return null;
    }
}
